/*
 * Game.cpp
 *
 *  Created on: 23.06.2012
 *      Author: sebastian
 */

#include "Game.h"

using namespace std;

Game::Game() {
	universe_ = new Universe();
}

void Game::init() {
	//TODO make it possible to save game stats...
#ifdef DEBUG
	// generate dummyplayers Alice and Bob:
	Player *alice;
	Player *bob;

	alice = new Player("Alice");
	bob = new Player("Bob");

	alice->setPassword("alice");
	bob->setPassword("bob");


	players_.push_back(alice);
	players_.push_back(bob);
#endif
	universe_->start();

}

Game::~Game() {
	// TODO Auto-generated destructor stub
	universe_->stop();
	//TODO save universe and players
}

Universe * Game::getUniverse() {
	return universe_;
}

bool Game::login(const std::string &name, const std::string &pass, const std::string &IP) { //returns if logging in worked...
	//check whether host is ok...
	if ((IP == "") || (IP.length() > 17))
		return false;
	Player * player = getPlayerByName(name);
	if (player == NULL)
		return false;
	if (player->getPassword() != pass)
		return false;
	else {
		player->connectTo(IP);
		return true;
	}
}


Player * Game::getPlayerByName(const std::string &name) {
	vector<Player*>::iterator it;
	it = players_.begin();
	while (( it != players_.end() ) && ( (*it)->getName() != name ))
		it++;

	return ( (it != players_.end()) ? (*it) : NULL);
}


Player * Game::getPlayerBySessionID(const int sessId) {
	vector<Player*>::iterator it;
	it = players_.begin();
	while (( it != players_.end() ) && ( (*it)->getSessionID() != sessId ))
		it++;

	return ( (it != players_.end()) ? (*it) : NULL);
}

Time Game::getTime() {
	return universe_->getTime(); //TODO: return real time!
}
